import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Base_UserCardServiceProxy, Base_UserCardDto } from '@shared/service-proxies/stock-service-proxies';
import { LazyLoadEvent } from 'primeng/api';
import { Paginator } from 'primeng/primeng';
import { Table } from 'primeng/table';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { CreateOrEditUserCardModalComponent } from './create-or-edit-user-card-modal.component';
@Component({
    templateUrl: './user-cards.component.html',
    animations: [appModuleAnimation()]
})
export class UserCardsComponent extends AppComponentBase {
    @ViewChild('createOrEditUserCardModal') createOrEditUserCardModal: CreateOrEditUserCardModalComponent;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    filterText = '';

    constructor(injector: Injector, private _userCardService: Base_UserCardServiceProxy) {
        super(injector);
    }

    getUserCards(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event) || event.first == undefined) {
            this.paginator.changePage(0);

            return;
        }

        this.primengTableHelper.showLoadingIndicator();

        this._userCardService
            .getUserCards(
                this.filterText,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }

    deleteUserCard(card: Base_UserCardDto): void {
        let self = this;
        self.message.confirm('提示', '确定删除' + card.realName + '的信息吗？', isConfirmed => {
            if (isConfirmed) {
                this._userCardService.deleteUserCard(card.id).subscribe(() => {
                    this.getUserCards();
                    abp.notify.success(this.l('SuccessfullyDeleted'));
                });
            }
        });
    }
}
